package cn.edu.neu.softlab.jason.offer.jianzhioffer.datastructure;

import cn.edu.neu.softlab.jason.offer.jianzhioffer.datastructure.util.LinkedList;
import cn.edu.neu.softlab.jason.offer.jianzhioffer.datastructure.util.LinkedNode;

import java.util.Stack;

/**
 * 剑指offer-面试5
 * 从尾到头打印链表
 * 输入一个链表的头结点，从尾到头反过来打印出每个结点的值
 */
public class Mianshi5 {
    public static void main(String[] args) {
        LinkedNode head = new LinkedNode(1);
        LinkedList.addToTail(head, 2);
        LinkedList.addToTail(head, 3);
        LinkedList.addToTail(head, 5);
        reversePrintRecursive(head);
    }

    public static void reversePrint(LinkedNode head) {
        Stack<Integer> stack = new Stack<>();
        LinkedNode node = head;
        while (node != null) {
            stack.push(node.value);
            node = node.next;
        }
        while (!stack.empty()) {
            System.out.print(stack.pop() + ",");
        }
    }

    public static void reversePrintRecursive(LinkedNode node) {
        if (node == null) {
            return;
        } else {
            reversePrintRecursive(node.next);
            System.out.print(node.value + ",");
        }

    }
}
